home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / rockridge / ui / overview / example / GUI.java < prev    next >
Encoding:
Java Source  |  1995-11-13  |  3.4 KB  |  142 lines

  1. import java.awt.*;
  2. import java.applet.Applet;
  3.  
  4. public class GUI extends Applet {
  5.     Frame window;
  6.  
  7.     public void init() {
  8.     Panel bottomPanel = new Panel();
  9.     Panel centerPanel = new Panel();
  10.  
  11.     setLayout(new BorderLayout());
  12.     add("South", bottomPanel);
  13.     add("Center", centerPanel);
  14.  
  15.     //Add small things at the bottom.
  16.     bottomPanel.add(new TextField("TextField"));
  17.     bottomPanel.add(new Button("Button"));
  18.     bottomPanel.add(new Checkbox("Checkbox"));
  19.     Choice c = new Choice();
  20.     c.addItem("Choice Item 1");
  21.     c.addItem("Choice Item 2");
  22.     c.addItem("Choice Item 3");
  23.     bottomPanel.add(c);
  24.  
  25.     //Add big things to the center area.
  26.     centerPanel.setLayout(new GridLayout(1,2));
  27.     //Put a canvas in the left column.
  28.     centerPanel.add(new MyCanvas());
  29.     //Put a label and a text area in the right column.
  30.     Panel p = new Panel();
  31.     p.setLayout(new BorderLayout());
  32.     p.add("North", new Label("Label", Label.CENTER));
  33.     p.add("Center", new TextArea("TextArea", 5, 20));
  34.     centerPanel.add(p);
  35.     }
  36.  
  37.     public void start(){
  38.     //Create a window with a menu at the top.
  39.     window = new MyWindow("Frame");
  40.  
  41.     window.pack();
  42.     Rectangle bounds = bounds();
  43.     Rectangle wbounds = window.bounds();
  44.     //window.move(bounds.x + (bounds.width - wbounds.width)/2,
  45.         //bounds.y + (bounds.height - wbounds.height)/2);
  46.     window.move(bounds.x + bounds.width + 15, bounds.y);
  47.     window.show();
  48.     }
  49.  
  50.     public static void main(String args[]) {
  51.     Frame f = new Frame("GUI Applet/Application");
  52.     GUI gui = new GUI();
  53.  
  54.     Font oldFont = gui.getFont();
  55.     if (oldFont == null) {
  56.         System.out.println("Eek! font is null!");
  57.     } else {
  58.         gui.setFont(new Font(oldFont.getFamily(), oldFont.getStyle(),
  59.                     oldFont.getSize()+2));
  60.     }
  61.  
  62.     gui.init();
  63.  
  64.     f.add("Center", gui);
  65.     f.resize(300, 300);
  66.     f.pack();
  67.     f.show();
  68.  
  69.     gui.start();
  70.     }
  71.  
  72. }
  73.  
  74. class MyCanvas extends Canvas {
  75.  
  76.     public void paint(Graphics g) {
  77.     int w = size().width;
  78.     int h = size().height;
  79.     g.drawRect(0, 0, w - 1, h - 1);
  80.     g.drawString("Canvas", (w - g.getFontMetrics().stringWidth("Canvas"))/2,
  81.               10);
  82.  
  83.     g.setFont(new Font("Helvetica", Font.PLAIN, 8));
  84.     g.drawLine(10,10, 100,100);
  85.     g.fillRect(9,9,3,3);
  86.     g.drawString("(10,10)", 13, 10);
  87.     g.fillRect(49,49,3,3);
  88.     g.drawString("(50,50)", 53, 50);
  89.     g.fillRect(99,99,3,3);
  90.     g.drawString("(100,100)", 103, 100);
  91.     }
  92.  
  93.     public Dimension minimumSize() {
  94.     return new Dimension(120,120);
  95.     }
  96.  
  97.     public Dimension preferredSize() {
  98.     return minimumSize();
  99.     }
  100. }
  101.  
  102. class MyWindow extends Frame {
  103.     final String FILEDIALOGMENUITEM = "File dialog...";
  104.     MyWindow(String title) {
  105.     super("Frame");
  106.     MenuBar mb = new MenuBar();
  107.     Menu m = new Menu("Menu");
  108.     m.add(new MenuItem("Menu item 1"));
  109.     m.add(new CheckboxMenuItem("Menu item 2"));
  110.     m.add(new MenuItem("Menu item 3"));
  111.     m.add(new MenuItem("-"));
  112.     m.add(new MenuItem(FILEDIALOGMENUITEM));
  113.     mb.add(m);
  114.     setMenuBar(mb);
  115.  
  116.     //Put a list in the window.
  117.     List l = new List(3, false);
  118.     l.addItem("List item 1");
  119.     l.addItem("List item 2");
  120.     l.addItem("List item 3");
  121.     l.addItem("List item 4");
  122.     l.addItem("List item 5");
  123.     l.addItem("List item 6");
  124.     l.addItem("List item 7");
  125.     add("Center", l); 
  126.     }
  127.  
  128.     public boolean action(Event evt, Object obj) {
  129.     if (evt.target instanceof MenuItem) {
  130.         String label = (String)obj;
  131.         if (label.equals(FILEDIALOGMENUITEM)) {
  132.         FileDialog fd = new FileDialog(this, "FileDialog");
  133.         //fd bounds are 0,0,0,0! Sometimes it shows up too skinny.
  134.         fd.show();
  135.         }
  136.     } else if (evt.id == Event.WINDOW_DESTROY) {
  137.         hide();
  138.     } 
  139.     return true;
  140.     }
  141. }
  142.